<?php

namespace App\Http\Controllers\Admin;

use App\Models\Ads;
use Illuminate\Http\Request;

use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Response;
use App\Models\AdsPosition;
use Illuminate\Support\Facades\Redirect;

class AdsPositionController extends BaseController
{
    
    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
        if(!$this->user->can('viewContentPosition'))
            abort(405);
    
        $adspositons = AdsPosition::orderBy('id', 'DESC')->paginate(20);
        return view('admin.adsposition.index')->with('adsposition', $adspositons);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create()
    {
        if(!$this->user->can('editContentPosition'))
            abort(405);
           
        return view('admin.adsposition.add');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  Request  $request
     * @return Response
     */
    public function store(Request $request)
    {
        if(!$this->user->can('editContentPosition'))
            abort(405);
        
        $id = Input::get('id');
        if ($id > 0)
            $ads_position = AdsPosition::find($id);
        else
            $ads_position = new AdsPosition;
        /*处理表单POST来的数据*/
        foreach (array_except(Input::all(), ['_token','id']) as $key => $val) {
            $ads_position->$key = $val;
        }

        if ($ads_position->save()) {
            return redirect("admin/content/position");
        } else {
            return Redirect::back()->withInput()->withErrors('保存失败!');
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function show($id)
    {
        if(!$this->user->can('viewContentPosition'))
            abort(405);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function edit($id)
    {
        if(!$this->user->can('viewContentPosition'))
            abort(405);
        
        $ads_position = AdsPosition::find($id);
        return view('admin.adsposition.edit')->with('adsposition', $ads_position);

    }

    /**
     * Update the specified resource in storage.
     *
     * @param  Request  $request
     * @param  int  $id
     * @return Response
     */
    public function updateStatus(Request $request)
    {
        if(!$this->user->can('editContentPosition'))
            return $this->formatJson('error','抱歉，您没有相关权限');
        
        $id = $request->input('id');
        $is_disabled = $request->input('is_disabled');
        $ads_position = AdsPosition::find($id);

        $ads_position->is_disabled = $is_disabled = $is_disabled > 0 ? 0 : 1;
        if ($ads_position->save())
            return  Response::json(array('status'=>'success','msg'=>'广告位', 'data' => $is_disabled));
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy($id)
    {
        if(!$this->user->can('deleteContentPosition'))
            return $this->formatJson('error','抱歉，您没有相关权限');
        
        $adspostion = AdsPosition::find($id);
        $position_is_used = Ads::where('position_id', $id)->first();
        if ($position_is_used)
            return  Response::json(array('status'=>'error','msg'=>'该广告位已经被使用，不能被删除！'));
        if ($adspostion->delete())
            return  Response::json(array('status'=>'success','msg'=>'广告位 [' . str_limit($adspostion->title, 15, '...') . '] 删除成功'));
        else
            return  Response::json(array('status'=>'error','msg'=>'删除失败'));
    }
}
